class Task < ActiveRecord::Base
  belongs_to :user, :class_name => "User", :foreign_key => "user_id"
  belongs_to :project
  belongs_to :assigned_by, :class_name => "User", :foreign_key => "assigned_by_id"
  
  def self.add_task(project_name, task_name, due_date, created_date, assign_to, twitter_user_id, time_spent)
    user = User.find_by_twitter_user_id(twitter_user_id)

    unless user.nil?
      Task.transaction do
        task = Task.new
        task.name = task_name
        task.created_at = created_date
        task.status = TASK_STATUS_OPEN

        # if due dates provided
        unless due_date.blank?
          task.due_date = due_date
        end

        # if we have a project name
        unless project_name.blank?
          project = Project.find_or_create_by_name_and_user_id(project_name, user.id)

          task.project = project
        end

        # if it's to be assigned to someone
        if ( !assign_to.blank? && assign_to != 0)
          assign_to_user = User.find_by_twitter_user_id(assign_to)
          if user.company_id.nil? || assign_to_user.nil? || assign_to_user.company_id != user.company_id
            return 'User cannot assign task.'
          end

          task.user_id = assign_to_user.id
          task.assigned_by_id = user.id
        else
          task.user_id = user.id
        end

        # if time spent has been provided
        unless time_spent.nil?
          task.time_spent = time_spent
        end

        task.save!
      end

      return 'Task added.'
    else
      return 'user does not exist'
    end
  end

  def self.match(name_parts, twitter_user_id)
    parts = name_parts.split
    query = parts.split.map {|term| "name like ?" }.join(' AND')

    tasks = Task.where("twitter_user_id = ? AND status = '?'", twitter_user_id, TASK_STATUS_OPEN).where(query, parts)

    task = nil
    unless tasks.length <= 0 || tasks.length > 1
      task = tasks[1]
    end
    return task
  end
  
  def self.complete(name, twitter_user_id, time_spent)
    task = Task.match(name, twitter_user_id)

    if !task.nil?
      task.status = TASK_STATUS_COMPLETED
      task.completed_at = Date.now
      unless time_spent.nil?
        task.time_spent = time_spent
      end
      task.save!
    else
     'Task could not be matched.'
    end
  end
  
  def self.postpone(name, twitter_user_id,new_due_date)
    task = Task.match(name, twitter_user_id)

    if !task.nil?
      task.due_date = new_due_date unless new_due_date.nil?
      task.status = TASK_STATUS_OPEN
      task.completed_at = nil
      task.save!
    else
      'Task could not be matched.'
    end
  end

  def toggle_status
    if self.status == TASK_STATUS_OPEN
      self.status = TASK_STATUS_COMPLETED
      self.completed_at = DateTime.now
    else
      self.status = TASK_STATUS_OPEN
      self.completed_at = nil
    end

    self.save!
  end

  def edit (task_name, project_name, due_date, assign_to, owner, time_spent)

    user = User.find(owner)

    self.name = task_name
    if !project_name.blank?
      project = Project.find_or_create_by_name_and_user_id(project_name, user.id)
      self.project = project
    else
      self.project = nil
    end
    self.due_date = due_date

    # if it's to be assigned to someone
    if ( !assign_to.blank? && assign_to != 0)
      assign_to_user = User.find(assign_to)
      if user.company_id.nil? || assign_to_user.nil? || assign_to_user.company_id != user.company_id
        return 'User cannot assign task.'
      end

      self.user_id = assign_to_user.id
      self.assigned_by_id = user.id
    else
      self.user_id = user.id
    end

    unless time_spent.nil?
      self.time_spent = time_spent
    end

    self.save!

  end
end
